package com.zygh.policeasset.controller;

import com.github.pagehelper.util.StringUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Accounts;
import com.zygh.policeasset.service.AccountsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("审计决算")
@RequestMapping("/accounts")
@RestController
public class AccountsController {

    @Autowired
    AccountsService accountsService;


    @RequestMapping("/add")
    @ApiOperation("添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectNo" ,value = "项目编号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "auditTime" ,value = "审计决算日期", required = true, paramType = "query"),
            @ApiImplicitParam(name = "description" ,value = "说明", required = false, paramType = "query"),
    })
    public Object add(Accounts accounts){
      if(StringUtil.isEmpty(accounts.getProjectNo())){
        return new CommonResult().validateFailed("项目ID不能为空");
        }
        if(StringUtil.isEmpty(accounts.getAuditTime())){
            return new CommonResult().validateFailed("审计时间不能为空");
        }
    int count = accountsService.add(accounts);
        if(count>0){
            return  new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }
@ApiOperation("查询所有")
@RequestMapping("/findall")
@ApiImplicitParams({
        @ApiImplicitParam(name = "projectNo" ,value = "项目编号", required = false, paramType = "query"),
        @ApiImplicitParam(name = "status" ,value = "审批状态", required = false, paramType = "query"),
        @ApiImplicitParam(name = "startTime" ,value = "查询审计决算开始时间", required = false, paramType = "query"),
        @ApiImplicitParam(name = "endTime" ,value = "查询审计决算结束时间", required = false, paramType = "query"),
        @ApiImplicitParam(name = "pageNum" ,value = "页数", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize" ,value = "条数", required = true, paramType = "query")
})
public Object findAll(Accounts accounts,@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                      @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
    return new CommonResult().success(accountsService.findAll(accounts,pageNum,pageSize));
}

    @ApiOperation("删除")
    @RequestMapping("/delete")
    @ApiImplicitParam(name = "id",value = "唯一主键",required = true,paramType = "query")
    public Object delete(Integer id){
        if(StringUtil.isEmpty(String.valueOf(id))){
            return new CommonResult().validateFailed("id不能为空");
        }
        int count = accountsService.delete(id);
        if(count>0){
            return  new CommonResult().success();
        }
        return  new CommonResult().validateFailed("删除失败");
    }
    @ApiOperation("通过ID查询")
    @RequestMapping("/selectbyid")
    @ApiImplicitParam(name = "id",value = "唯一主键",required = true ,paramType = "query")
    public Object selectById(Integer id){
        if(StringUtil.isEmpty(String.valueOf(id))){
            return new CommonResult().validateFailed("id不能为空");
        }
        Accounts settlement = accountsService.selectById(id);
        return new CommonResult().success(settlement);
    }

    @ApiOperation("更新")
    @RequestMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectNo" ,value = "项目编号", required = false, paramType = "query"),
            @ApiImplicitParam(name = "auditTime" ,value = "审计决算日期", required = false, paramType = "query"),
            @ApiImplicitParam(name = "description" ,value = "说明", required = false, paramType = "query"),
            @ApiImplicitParam(name = "id" ,value = "唯一主键", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status",value = "状态（0是未提交，1是已提交，2是已通过，3是已拒绝）",required = false,paramType = "query")
    })
    public  Object update(@RequestBody Accounts accounts){
        if(StringUtil.isEmpty(accounts.getProjectNo())){
            return  new CommonResult().validateFailed("项目编号不能为空");
        }
        if(StringUtil.isEmpty(accounts.getAuditTime())){
            return  new CommonResult().validateFailed("审计决算日期不能为空");
        }

        int count = accountsService.update(accounts);
        if(count>0){
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新失败");
    }



}
